简体   繁体   English

如何在数字的字符串中扫描以调整iOS应用程序中的字体大小?

[英]How can I scan a string for numbers to adjust the font size in an iOS app?

I'm off to a good start. 我有一个好的开始。 But I would like to make this code more dynamic. 但我想使这段代码更具动态性。

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont systemFontOfSize:10.0f];
UIFont *smallFont = [UIFont systemFontOfSize:9.0f];

[attString beginEditing];
[attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];

[attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];

Say I have the following String: 说我有以下字符串:

@"C3OC2OH4" @“ C3OC2OH4”

I would like to adjust the font size of the numbers only to fulfill a chemistry application. 我只想调整数字的字体大小即可实现化学应用。 How can I scan the above string, and create a series of custom ranges to plug into my function above, which adjusts the font size? 如何扫描上面的字符串,并创建一系列自定义范围以插入到上面的函数中,从而调整字体大小?

You can enumerate the characters and apply the attributes like this. 您可以枚举字符并像这样应用属性。

NSString * string = @"C3OC2OH4";

NSMutableAttributedString * attributedString = [NSMutableAttributedString new];

NSDictionary * subscriptAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:10.0],
                                       NSBaselineOffsetAttributeName : @(-5.0)};

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                           options:NSStringEnumerationByComposedCharacterSequences
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

                            NSCharacterSet * letterCharacterSet = [NSCharacterSet letterCharacterSet];

                            if ([substring rangeOfCharacterFromSet:letterCharacterSet].location != NSNotFound)
                                [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring]];
                            else
                                [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring attributes:subscriptAttributes]];

                        }];

In this case substring is each character and is appended without any change if it is a letter. 在这种情况下, substring是每个字符,并且如果是字母,则不加任何更改地附加到substring中。 If it is a number we change the font size and shift the baseline. 如果是数字,我们将更改字体大小并移动基线。 Change them according to your need. 根据您的需要进行更改。

Good luck. 祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PhoneGap:iOS 7中的UIPickerView不会像在iOS 6中那样自动调整字体大小。如何在iOS 7中实现的任何解决方案? - PhoneGap : UIPickerView in iOS 7 does not adjust font size automatically as it does in iOS 6. Any solution for how can I achieve in iOS 7? 如何根据字符串的长度和标签大小调整标签的字体大小 - How to adjust font size of a label according to length of string and label size 如何在iOS中为各种iPhone屏幕尺寸调整字体大小? - How to adjust font size for various iPhone screen sizes in iOS? 如何在iOS上调整此相机应用的框架? - How can I adjust the frame for this camera app on iOS? 我如何知道 Swift 的 iOS 字体大小(或百分比)? - How can I know the font size (or percent) of the iOS with Swift? iOS,Swift:如何允许读者使用导航栏按钮项来调整WKWebView中的正文字体大小? - iOS, Swift: How do I allow readers to use Navigation Bar Button Items to adjust body font size in a WKWebView? 如何在iOS中调整iPad的字体大小? (冲突元素) - How do I adjust the font sizes for iPad in iOS? (Clashing elements) 如何使旧应用程序状态栏的字体大小与iOS 10中的系统状态栏匹配? - How can I make the font size of the status bar of my old app to match the system status bar in iOS 10? iOS如何根据屏幕的大小类调整UILabel或UITextField字体大小? - iOS how to adjust UILabel or UITextField font size based on screen's size class? 如何在 iOS 上扫描条形码? - How can I scan barcodes on iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM