Measuring the pixel width of a string

后端 未结 3 759
猫巷女王i
猫巷女王i 2020-11-29 05:45

I need to measure the pixel width of a string in Cocoa Touch. Can anyone point me to a link that explains how to do this?

3条回答
  •  我在风中等你
    2020-11-29 06:13

    The above solution is now deprecated. Check this for iOS 7+

     sizeWithFont:constrainedToSize:lineBreakMode 
    

    is deprecated now. Use below code snippet,

    UIFont *font=[UIFont fontWithName:@"Arial" size:16.f];
    
    NSString *name = @"APPLE";
    
    CGSize size = [name sizeWithAttributes:@{NSFontAttributeName:font}];
    

提交回复
热议问题