gpt4 book ai didi

iphone - iOS:同一个词的粗体和斜体

转载 作者:可可西里 更新时间:2023-11-01 03:39:29 25 4
gpt4 key购买 nike

背景:我一直在尝试用粗体和斜体以及普通字体来显示一个句子。

问题:如何显示“你好,我叫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:同一个词的粗体和斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10078531/

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