Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UILabel中显示不同的颜色和字体类型_Ios_Objective C_Uilabel - Fatal编程技术网

Ios 如何在UILabel中显示不同的颜色和字体类型

Ios 如何在UILabel中显示不同的颜色和字体类型,ios,objective-c,uilabel,Ios,Objective C,Uilabel,我有一根绳子 NSSting *myString=@"First Second Third Fourth Fifth Sixth Seventh"; 我想用不同的字体类型和颜色在单个标签中显示此字符串 i、 e “First”字符串应为粗体 “第二个”字符串应为斜体 “第三个”字符串应为红色 “第四个”字符串应为绿色 “第五个”字符串应为系统字体的粗体斜体 “第六个”字符串应为arial粗体 “第七”字符串应为arial粗体,大小为34 我已经阅读了stackoverflow中的一些答案,但这

我有一根绳子

NSSting *myString=@"First Second Third Fourth Fifth Sixth Seventh";
我想用不同的字体类型和颜色在单个标签中显示此字符串

i、 e

“First”字符串应为粗体

“第二个”字符串应为斜体

“第三个”字符串应为红色

“第四个”字符串应为绿色

“第五个”字符串应为系统字体的粗体斜体

“第六个”字符串应为arial粗体

“第七”字符串应为arial粗体,大小为34

我已经阅读了stackoverflow中的一些答案,但这些答案中没有任何清晰之处

有谁能提供一些指导吗

提前谢谢

解决方案:首先根据您的需求操作
nsattributed字符串

使用
NSAttributedString
u可以在
ios6uilabel
has
attributedString属性中执行此操作


ios 6.0以下的
使用或任何
第三方attributedLabel
在ios 6.0及以上的
中显示attributedString
具有以下属性:
@属性(非原子,复制)NSAttributedString*attributedText

接受
nsattributed字符串的

您可以创建一个符合您要求的
NSAttributedString

试试

也看到


希望我能帮助您使用此代码为不同的单词设置不同的颜色

NSString *test = @"First Second Third Fourth Fifth Sixth Seventh";

 CFStringRef string =  (CFStringRef) test;
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string);



    /*
     Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its
     attributes right when we create it. We can change them if we use mutable form of CFAttributeString.
     */



    //Lets choose the colors we want to have in our string
    CGColorRef _orange=[UIColor orangeColor].CGColor;
    CGColorRef _green=[UIColor greenColor].CGColor;
    CGColorRef _red=[UIColor redColor].CGColor;
    CGColorRef _blue=[UIColor blueColor].CGColor;    




    //Lets have our string with first 20 letters as orange
    //next 20 letters as green
    //next 20 as red
    //last remaining as blue
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);    
    CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue);
您还可以设置图像的颜色,如下图所示

[yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]];
看到我的另一个答案了吗


我希望这对您有所帮助……

在iOS<6上为我完成了这项工作

如果不使用NSAttributeLabel,则 试试这个,对我来说很好

将此方法添加到CommonFunction类并添加此框架CoreText.framework

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString label:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName
{
    label.layer.sublayers = nil;

    NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text];

    for (int i =0 ; i<[rangeString count]; i++)
    {
        CTFontRef  ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].pointSize, NULL);

        NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]];

        if (whiteRange.location != NSNotFound)
        {
            [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange];
            [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName
                                            value:( __bridge id)ctFont
                                            range:whiteRange];
        }
    }

    CGSize expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap];

    CATextLayer   *textLayer = [[CATextLayer alloc]init];
    textLayer.frame =CGRectMake(0,0, label.frame.size.width,expectedLabelSize.height+4);
    textLayer.contentsScale = [[UIScreen mainScreen] scale];
    textLayer.string=mutableAttributedString;
    textLayer.opacity = 1.0;
    textLayer.alignmentMode = kCAAlignmentLeft;
    [label.layer addSublayer:textLayer];
    [textLayer setWrapped:TRUE];

    //label.lineBreakMode = UILineBreakModeWordWrap;

    label.text = @"";
}
我希望它会为你工作良好


`

我在开发过程中也遇到了同样的问题。由于NSAttributeString在上面的ioS6 n以上工作。而不是获得第三方类, 我建议您创建支持自定义标签的自定义UILabel 简而言之,通过指定颜色、粗体和长度来创建许多标签(逐字)。
并将所有标签附加在一起,然后创建一个新的标签对象

您不能在单个标签中执行此操作。您需要7个不同的标签使用NSAttributedString u可以在ios6中实现uilabel具有attributedString属性。在ios6.0下,使用TTAttributeLabel显示attributedString。我在上发现了许多问题,那么,在ios6.0以下的版本中做些什么呢???检查这个@Prince:你说什么?请详细解释
[CommonFunctions setMultiColorAndFontText:string rangeString:[NSArray arrayWithObjects:str1,str2,str3,str4,str5,str6, nil] label:yourLabel font:[NSArray arrayWithObjects:@"Arial",@"Arial",@"Arial",@"Arial",@"Arial",@"Arial"nil] color:[NSArray  arrayWithObjects:(UIColor *)[UIColor colorWithRed:(0/255.f) green:(167/255.f) blue:(230/255.f) alpha:1.0f].CGColor,(UIColor *)[UIColor colorWithRed:(189/255.f) green:(189/255.f) blue:(189/255.f) alpha:1.0f].CGColor, color3,color4,color5,color6,nil]];