在iOS中,如何在textView中展示不同字体的文本?

3

你好,我正在开发一个应用程序。在这个应用中,我需要展示不同字体的文本。我的意思是,如果我想要用一种字体显示名字,那么我可以用另一种字体来显示城市名。

4个回答

3
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 100)];

textView.text = @"Vijay Apple Dev";

NSString *textViewText = textView.text;

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:textViewText];

[attributedText addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:@"Courier" size:20]
                       range:[textViewText rangeOfString:@"Apple"]];


[attributedText addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:@"Verdana" size:20]
                       range:[textViewText rangeOfString:@"Vijay"]];

textView.attributedText = attributedText;

[self.view addSubview:textView];

enter image description here


2

试着像这样做...并根据您的要求进行自定义,如字体名称和字体大小

        NSString *firstName = @"FirstName";

        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:firstName];

        UIFont *font = [UIFont fontWithName:@"Helvetica" size:20];

        [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, firstName.length)];// Here define your range.

        textView.attributedText = attributedString;

2
//Use attributed String

//Apply properties to both strings 

//then merge and set it to textview

NSString *selectedString=@"Hey,";  

UIFont *fontSelectedText = [UIFont boldSystemFontOfSize:25];

NSDictionary *dictBoldSelectedText = [NSDictionary dictionaryWithObjectsAndKeys:fontSelectedText, NSFontAttributeName, nil];

NSMutableAttributedString *mutAttrTextViewSelectedString = [[NSMutableAttributedString alloc] initWithString:selectedString];

[mutAttrTextViewSelectedString setAttributes:dictBoldSelectedText range:NSMakeRange(0,selectedString.length)];

[_tTextView setAttributedText:mutAttrTextViewSelectedString];


//Second String
NSString *restOfStrig=@"\nHello";
NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:restOfStrig];

UIFont *fontText = [UIFont boldSystemFontOfSize:16];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];

[mutAttrTextViewString setAttributes:dictBoldText range:NSMakeRange(0,restOfStrig.length)];

//Combining both the Attributed String
[mutAttrTextViewSelectedString appendAttributedString:mutAttrTextViewString];

NSMutableAttributedString * finalAttributedStr=[[NSMutableAttributedString alloc] initWithAttributedString:mutAttrTextViewSelectedString];

//setting it to the textView
[_tTextView setAttributedText:finalAttributedStr];

0
使用 `NSAttributedString`
"NSAttributedString" 对象管理字符字符串和与其关联的一组属性(例如字体和字距),这些属性适用于字符串中的单个字符或字符范围。字符和它们的属性的关联称为属性字符串。该集群的两个公共类,NSAttributedString 和NSMutableAttributedString,分别声明只读属性字符串和可修改属性字符串的编程接口。 NSAttributedString 当您创建自己的属性字符串后,只需将其设置为textView: `self.textView.attributedText = myAttributedString;`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接