gpt4 book ai didi

objective-c - 有没有办法做一半的换行符?

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:24 24 4
gpt4 key购买 nike

我想知道,有没有一种方法可以在 Objective-C 的 NSString 中做一半的换行符 (\n),即它只跳过大约一半的空间?或者以其他方式在 NSString 中完成此操作?

最佳答案

如 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;

如果您最终使用 TTTAtributedLabel,您将使用 label.text = attrStr; 或其中一种辅助方法(摘自 TTTAtributedLabel 文档:

[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;
}];

此外,TTTAtributedLabel 有一个 lineHeightMultiple(介于 0.0 和 1.0 之间)属性,您可以调整该属性以获得所需的效果。这样,您仍然可以使用 NSString 而不会弄乱有时丑陋的 NSAttributedString

关于objective-c - 有没有办法做一半的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com