我想知道,有没有办法在目标C中做一半的换行符(\n
)NSString
,即它只跳过大约一半的空间?或者无论如何以其他方式完成此操作NSString
?
问问题
1140 次
1 回答
3
就像 Wain 所说,在 NSAttributedString 上设置 NSParagraphStyle 可能是您要寻找的。UILabel 在 iOS 6 中支持 NSAttributedStrings,但在此之前您必须使用第三方组件。TTTAttributedLabel非常好并且有据可查。
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"Hello World!"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:24]; //Just a random value, you'll have to play with it till you are hhappy
[attrStr addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, [myString length])];
label.attributedText = attrStr;
如果您最终使用TTTAttributedLabel
您将使用label.text = attrStr;
或其中一种辅助方法(取自TTTAttributedLabel
文档:
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"ipsum dolar" options:NSCaseInsensitiveSearch];
NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch];
// Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange];
[mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:YES] range:strikeRange];
CFRelease(font);
}
return mutableAttributedString;
}];
此外,TTTAttributedLabel
还有一个lineHeightMultiple
(介于 0.0 和 1.0 之间)属性,您可以使用它来获得所需的效果。这样,您仍然可以使用 anNSString
而不会与有时丑陋的NSAttributedString
.
于 2013-08-12T15:30:13.003 回答