![](/img/trans.png)
[英]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 can I scan a string for numbers to adjust the font size in an iOS app?
我有一個好的開始。 但我想使這段代碼更具動態性。
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)];
說我有以下字符串:
@“ C3OC2OH4”
我只想調整數字的字體大小即可實現化學應用。 如何掃描上面的字符串,並創建一系列自定義范圍以插入到上面的函數中,從而調整字體大小?
您可以枚舉字符並像這樣應用屬性。
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]];
}];
在這種情況下, substring
是每個字符,並且如果是字母,則不加任何更改地附加到substring
中。 如果是數字,我們將更改字體大小並移動基線。 根據您的需要進行更改。
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.