xcode iOS:同一个字上的粗体和斜体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10078531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Bold and Italic on the same word
提问by Byte
Background: I have been trying to display a sentence with Bold and Italic font as well as normal ones.
背景:我一直在尝试用粗体和斜体以及普通字体显示一个句子。
Question: How can I display something like this "Hello, my name is Byte". Notice that Byte is both bold and italic, while other words remains normal.
问题:如何显示类似“你好,我的名字是Byte”的内容。请注意,Byte 既是粗体又是斜体,而其他字词保持正常。
I have tried: I think coreText should be able to do something along the line, I just have not been able to find the correct way to do it. I also used TTTAttributeLabel and cannot make it both bold and italic. I have Three20 loaded, just do not know which or what to use. Webview does not work with my background.
我试过:我认为 coreText 应该能够做一些事情,我只是找不到正确的方法来做到这一点。我还使用了 TTTAttributeLabel 并且不能将其设为粗体和斜体。我已经装了 Three20,只是不知道用哪个或什么。Webview 不适用于我的背景。
As a reply to Carles Estevadeordal:
作为对 Carles Estevadeordal 的回复:
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 360, 300, 40)];
[webView setBackgroundColor: [UIColor clearColor]];
[webView loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];
[self.view addSubview:webView];
This is exactly the code, I used. It displayed white background.
这正是我使用的代码。它显示白色背景。
回答by Byte
After a good night sleep, I found a way to do it using TTTAtributedlabel. Here is how:
一夜好眠后,我找到了一种使用TTTAtributedlabel 的方法。方法如下:
TTTAttributedLabel *attLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(x, y, xx, yy)];
NSString *text = @"Hello, my name is Byte";
[attLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^(NSMutableAttributedString *mutableAttributedString) {
//font helvetica with bold and italic
UIFont *boldSystemFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:10];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"Byte" options:NSCaseInsensitiveSearch];
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
CFRelease(font);
return mutableAttributedString;
}];
I still do not have a way to add 2 attributes (ie: bold and italic separately) into the same word/letter. But this does the trick.
我仍然没有办法将 2 个属性(即:粗体和斜体分开)添加到同一个单词/字母中。但这确实有效。
回答by TegRa
In case you are still interested in a CoreText solution to this issue :
如果您仍然对此问题的 CoreText 解决方案感兴趣:
newFont = CTFontCreateCopyWithSymbolicTraits(fontRef,
self.textSize,
NULL,
kCTFontBoldTrait | kCTFontItalicTrait,
kCTFontBoldTrait | kCTFontItalicTrait);
[attrString addAttribute:(NSString*)kCTFontAttributeName
value:(id)newFont
range:[copyString rangeOfString:customText.text]];
回答by J?hn Narr?w
You can use this where sender is instance of UIButton
您可以在发件人是 UIButton 实例的情况下使用它
sender.titleLabel.font=[UIFont fontWithName:@"Helvetica-BoldOblique" size:18.0f];
回答by Alastair Stuart
To add two attributes (this is non ARC code):
添加两个属性(这是非 ARC 代码):
#define kLabelFontSize 16.0
NSString labelString = @"Let's slide loudly";
[self.tttLabel setText:labelString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:NSMakeRange(6, 11)];
CFRelease(boldFont);
}
UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:kLabelFontSize];
CTFontRef italicFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, italicSystemFont.pointSize, NULL);
if (italicFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)italicFont range:NSMakeRange(12, 18)];
CFRelease(italicFont);
}
}];
return mutableAttributedString;
}];
回答by Carles Estevadeordal
If the sentence has a fixed format the easiest solution is to use 2 different UILabels, one with the normal font and the other one with a Bold and Italic font set.
如果句子有固定格式,最简单的解决方案是使用 2 个不同的 UILabels,一个使用普通字体,另一个使用粗斜体字体集。
If the sentence is dynamic then you should use a UIWebView object as it was a lavel and load a simple webpage with your text and the < b > < i > < /i > < /b > fields set at the spot when you want it bold and italic like:
如果句子是动态的,那么您应该使用 UIWebView 对象,因为它是一个 lavel 并加载一个简单的网页,其中包含您的文本和 < b > < i > </i > </b > 字段在您需要时设置在现场粗体和斜体,如:
[myWebViewLabel loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];
I hope it helps.
我希望它有帮助。