加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – iOS:Bold和Italic在同一个单词上

发布时间:2020-12-14 19:48:22 所属栏目:百科 来源:网络整理
导读:背景:我一直试图用Bold和Italic字体以及普通字体显示一个句子. 问题:如何显示类似“Hello,我的名字是Byte”的内容.请注意,Byte既是粗体又是斜体,而其他单词仍然正常. 我试过了:我认为coreText应该能够做到这一点,我只是无法找到正确的方法来做到这一点.我
背景:我一直试图用Bold和Italic字体以及普通字体显示一个句子.

问题:如何显示类似“Hello,我的名字是Byte”的内容.请注意,Byte既是粗体又是斜体,而其他单词仍然正常.

我试过了:我认为coreText应该能够做到这一点,我只是无法找到正确的方法来做到这一点.我也使用了TTTAttributeLabel,并且不能使它既粗体又斜体.我有Three20加载,只是不知道使用哪个或什么. Webview不适用于我的背景.

作为对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];

这正是我用过的代码.它显示白色背景.

解决方法

经过一夜好眠,我找到了一种方法,使用 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;
}];

我仍然无法将2个属性(即:粗体和斜体)分别添加到同一个单词/字母中.但这就是诀窍.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读