iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – iOS:Bold和Italic在同一个单词上大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

背景:我一直试图用Bold和Italic字体以及普通字体显示一个句子. 问题:如何显示类似“Hello,我的名字是Byte”的内容.请注意,Byte既是粗体又是斜体,而其他单词仍然正常. 我试过了:我认为coreText应该能够做到这一点,我只是无法找到正确的方法来做到这一点.我也使用了TTTAttributeLabel,并且不能使它既粗体又斜体.我有Three20加载,只是不知道使用哪个或什么.
背景:我一直试图用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个属性(即:粗体和斜体)分别添加到同一个单词/字母中.但这就是诀窍.

大佬总结

以上是大佬教程为你收集整理的iphone – iOS:Bold和Italic在同一个单词上全部内容,希望文章能够帮你解决iphone – iOS:Bold和Italic在同一个单词上所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。