如何在ios中显示不同字体的文本?

时间:2021-08-14 21:12:49

Hi I am doing one application. In that I need to show text in different fonts. I mean if I show name in one font and I show city name in different font.

你好,我在做一个申请。我需要用不同的字体显示文本。我的意思是如果我用一种字体显示名字,我用不同的字体显示城市名字。

4 个解决方案

#1


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];

如何在ios中显示不同字体的文本?

#2


2  

Try to do like this one... And customize according to your requirement like font name and font size

试着这样做……并根据您的要求定制,如字体名称和字体大小

        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;

#3


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];

#4


0  

Use NSAttributedString

使用NSAttributedString

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.

NSAttributedString对象管理字符字符串和相关的属性集(例如,字体和kerning),这些属性适用于字符串中的单个字符或字符范围。字符及其属性的关联称为带属性字符串。集群的两个公共类NSAttributedString和NSMutableAttributedString分别为只读带属性的字符串和可修改带属性的字符串声明编程接口。

NSAttributedString

NSAttributedString

When you create your attributed string, then just set it to your textView: self.textView.attributedText = myAttributedString;

创建带属性字符串时,只需将其设置为textView: self。textView。attributedText = myAttributedString;

#1


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];

如何在ios中显示不同字体的文本?

#2


2  

Try to do like this one... And customize according to your requirement like font name and font size

试着这样做……并根据您的要求定制,如字体名称和字体大小

        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;

#3


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];

#4


0  

Use NSAttributedString

使用NSAttributedString

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.

NSAttributedString对象管理字符字符串和相关的属性集(例如,字体和kerning),这些属性适用于字符串中的单个字符或字符范围。字符及其属性的关联称为带属性字符串。集群的两个公共类NSAttributedString和NSMutableAttributedString分别为只读带属性的字符串和可修改带属性的字符串声明编程接口。

NSAttributedString

NSAttributedString

When you create your attributed string, then just set it to your textView: self.textView.attributedText = myAttributedString;

创建带属性字符串时,只需将其设置为textView: self。textView。attributedText = myAttributedString;