gpt4 book ai didi

ios - Objective C 中 UILabel 文本的多个属性(不同的文本大小、字体和偏移量)

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:09 24 4
gpt4 key购买 nike

I want to do something similar like:

enter image description here

the text are not fixed they will change at run time.

需要你的想法!

提前致谢!

最佳答案

After searching a lot, I have end up with

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

previousRandom = -1;

// NEEDY TALL BORING COOL CHEAP AWESOME HANDSOME SEXY SMART SMELLY
NSString *strForText = @"NEEDY TALL BORING COOL CHEAP AWESOME HANDSOME SEXY SMART SMELLY HAPPY RICH FUNNY SEXY SMART SMELLY HAPPY RICH FUNNY";

self.labelForText.attributedText = [[NSAttributedString alloc]initWithString:strForText];

NSArray *arrayForText = [self.labelForText.text componentsSeparatedByString:@" "];

for (NSString *str in arrayForText) {

[self makeTextRandmolyFocused:self.labelForText andString:str];
}

}

// making the $ prefix
-(void)makeTextRandmolyFocused:(UILabel *)label andString:(NSString *)subString {

NSRange range = [label.text rangeOfString:subString];

NSArray *arrayForFontSize = @[@"18",@"11",@"16",@"13",@"14",@"20"];
NSArray *arrayForFontName = @[@"HelveticaNeue-Medium",@"HelveticaNeue-Light",@"HelveticaNeue-Bold",@"HelveticaNeue-Medium",@"HelveticaNeue-Medium",@"HelveticaNeue-Bold"];
NSArray *arrayForBaselineOffset = @[@"2.3",@"1.2",@"2.5",@"1.5",@"3.5",@"0"];

int randomPosition;

do {
randomPosition = arc4random_uniform(5);
} while (randomPosition == previousRandom);

previousRandom = randomPosition;

NSNumber *baselineOffSet =[NSNumber numberWithUnsignedInteger:[arrayForBaselineOffset[randomPosition] integerValue]];

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:3.0];
[style setAlignment:NSTextAlignmentCenter];

float spacing = 2.5f;


NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];

[attributedText setAttributes:@{
NSFontAttributeName:[UIFont fontWithName:arrayForFontName[randomPosition] size:[arrayForFontSize[randomPosition] floatValue]],
NSBaselineOffsetAttributeName:baselineOffSet,
NSParagraphStyleAttributeName:style,
NSKernAttributeName:@(spacing)
}
range:range];

label.attributedText = attributedText;

}

Which gives the output as:

enter image description here

关于ios - Objective C 中 UILabel 文本的多个属性(不同的文本大小、字体和偏移量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842145/

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