2016-08-10 1 views

Antwort

2

kann ich Ihnen ein Beispiel für dieses Bild zeigen:

enter image description here

Code:

- (NSMutableAttributedString *)styleSalePriceLabel:(NSString *)salePrice withFont:(UIFont *)font 
{ 
    if ([salePrice rangeOfString:@"."].location == NSNotFound) { 
     return [[NSMutableAttributedString alloc] initWithString:salePrice]; 
    } else { 
     NSRange range = [salePrice rangeOfString:@"."]; 
     range.length = (salePrice.length - range.location); 
     NSMutableAttributedString *stylizedPriceLabel = [[NSMutableAttributedString alloc] initWithString:salePrice]; 
     UIFont *smallFont = [UIFont fontWithName:font.fontName size:(font.pointSize/2)]; 
     NSNumber *offsetAmount = @(font.capHeight - smallFont.capHeight); 
     [stylizedPriceLabel addAttribute:NSFontAttributeName value:smallFont range:range]; 
     [stylizedPriceLabel addAttribute:NSBaselineOffsetAttributeName value:offsetAmount range:range]; 
     return stylizedPriceLabel; 
    } 
} 
+1

Dies ist die Lösung, die Sie verwenden sollten, da es ein Label verwendet und es einfacher ist, die Styles zu aktualisieren, ohne jedes Mal einen Layout-Code neu zu justieren. –

0

Schließlich .... Ich habe es

//VerticalSpace from SmallFont Label to largeFont Label 
@IBOutlet weak var dollarLabelTopSpacetoAmountLabel: NSLayoutConstraint! 

@IBAction func btnClicked() { 
    let amountString = self.amountLbl.text! as NSString 
    let fontSize = self.amountLbl.frame.size.width/CGFloat(amountString.length) 
    let difference = self.amountLbl.frame.size.height-fontSize 
    print(fontSize) 
    //Your minimum font size(30). 
    if fontSize>30 { 
     self.dollarLabelTopSpacetoAmountLabel.constant = -(difference- 
    }else{ 
     // dollarLabelTopSpacetoAmountLabel.constant = -30 
    } 

}

Verwandte Themen