简体   繁体   中英

Change paragraph height (not line spacing) in a UILabel

Is it possible to limit the distance between paragraphs that were created using \\n\\n smaller in UILabel s using attributed strings?

So for example, I would like this: 在此处输入图片说明

To look like this:

在此处输入图片说明

Would this involve replace \\n\\n with something else? Or is there a much simpler solution using NSAttributedString ?

First at all: The usage of \\n\\n to create distances between two paragraphs is no good idea at all. \\n has the semantic meaning of a new paragraph, so you have three paragraphs, where two are semantically meant. This is like a amateurish secretary deals with paragraph distances. You should replace them with a single \\n .

However, you should not use font sizes to adjust line spacing or paragraph spacing. This highly relies on the shape of a font and its definition. Things break fast.

Add a paragraph style, because they are built for paragraph spacing. Set the line height or paragraph spacing properties.

The solution I outlined in my comment works. You can set the font size of the the empty line / paragraph spacing as something that pleases your eyes:

[myAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:5.0] range:NSMakeRange(6, 1)];

The following code finds all occurrences of \\n\\n and specify the second one to have a specific size:

unsigned long length = myAttributedString.length;
NSRange range = NSMakeRange(0, length);
NSRange found;
while (NSNotFound != (found =[myAttributedString.string rangeOfString:@"\n\n" options:0 range:range]).location) {
    [myAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:8.0] range:NSMakeRange(found.location + 1, 1)];
    range = NSMakeRange(found.location + 2, length - found.location - 2);
}

One thing I didn't mention in the question, which I had thought was obvious from the example is that the description is not within my control, it is generated by users. Therefore, the carriage return characters are added by them when they are creating the text.

So the solution I came up with is the following:

Firstly, I replace any \\n\\n characters with a single carriage return. This was inspired by amin-negm-awad's answer. \\n\\n is not a desirable way to generate a paragraph space.

I am doing this using the following piece of code:

func sanitize() -> String {
    var output = NSMutableString(string: self)
    var numberOfReplacements = 0
    do {
        let range = NSMakeRange(0, output.length)
        numberOfReplacements = newString.replaceOccurrencesOfString("\n\n", withString: "\n", options: NSStringCompareOptions.CaseInsensitiveSearch, range: range)
    } while (numberOfReplacements > 0)
    return output as String
}

The next part is to apply a paragraph style with an attributed string. Here is an example function that is fairly flexible:

func textAttributesWithFont(font: UIFont, andColor color: UIColor,
    lineSpacing: CGFloat = 0,
    maximumLineHeight: CGFloat = 0,
    textAlignment: NSTextAlignment = .Natural) -> [NSObject: AnyObject] {
        var attributes = [NSFontAttributeName : font, NSForegroundColorAttributeName : color]
        var paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.alignment = textAlignment
        paragraphStyle.maximumLineHeight = maximumLineHeight
        paragraphStyle.paragraphSpacing = 4
        attributes[NSParagraphStyleAttributeName] = paragraphStyle
        return attributes
}

Finally the label is constructed using the attributes:

var label1 = UILabel()
let text1 = "This is a test that is supposed😓😛😠😑  to wrap with some paragaphs\n\nThis is a paragraph"
label1.attributedText = NSAttributedString(string:sanitizeComment(text1), attributes: attributes)
label1.numberOfLines = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM