iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
jtn007
V2EX  ›  iDev

求解关于 IOS 中一个 Label 自适应高度的问题。

  •  
  •   jtn007 ·
    Neo-J · Aug 29, 2014 · 13008 views
    This topic created in 4283 days ago, the information mentioned may be changed or developed.
    我现在程序中有一个label,宽度固定,高度需要根据获取到的文字的长度来决定,本来是有方法来根据string获取高度的,但是现在获取到的不是一个简单的字符串,而是一串html字符串,那么我就需要NSAttributedString去解析并保存它,可是这样的话就没法用之前那个方法去获取高度了,问下有没有什么好的解决方案。
    20 replies    2014-08-31 23:09:13 +08:00
    txx
        1
    txx  
       Aug 29, 2014
    AutoLayout..
    jtn007
        2
    jtn007  
    OP
       Aug 29, 2014
    @txx 可我不用xib啊。。。布局全是代码
    txx
        3
    txx  
       Aug 29, 2014
    @jtn007 呵呵...
    chmlai
        4
    chmlai  
       Aug 29, 2014
    @jtn007 autolayout不能用代码写吗?
    hustlzp
        5
    hustlzp  
       Aug 29, 2014
    @jtn007 手写constraint吧。
    openkava
        6
    openkava  
       Aug 29, 2014   ❤️ 1
    UILabel *lbTeseTitle2 = [[UILabel alloc ] initWithFrame:CGRectMake(36, 24, titleView.width-40 ,50 )] ;
    lbTeseTitle2.text = self.theItem[@"feature"] ;
    lbTeseTitle2.font = SYSTEMFONT(14) ;
    [lbTeseTitle2 setNumberOfLines: 0];
    [tempView addSubview:lbTeseTitle2 ] ;
    //设置一个行高上限
    CGSize size = CGSizeMake(titleView.width-40,2000);
    //计算实际frame大小,并将label的frame变成实际大小
    CGSize labelsize = [ lbTeseTitle2.text sizeWithFont:lbTeseTitle2.font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];

    [lbTeseTitle2 setFrame: CGRectMake(36, 24, labelsize.width, labelsize.height)];
    hrx00747
        8
    hrx00747  
       Aug 29, 2014   ❤️ 1
    设置 numberOfLines = 0
    调用 sizeToFit
    jtn007
        9
    jtn007  
    OP
       Aug 29, 2014
    @txx 呵呵是什么意思啊,我接触IOS开发不久。。求指教啊
    jtn007
        10
    jtn007  
    OP
       Aug 29, 2014
    @chmlai 我不太清楚,我刚接触这个不久,autolayout不是storyboard里打勾选的那个么?
    SeanChense
        11
    SeanChense  
       Aug 29, 2014
    @jtn007 感觉他在说不用 sb 的都是 sb
    txx
        12
    txx  
       Aug 29, 2014   ❤️ 1
    @SeanChense
    @jtn007 所有東西都是可以用代碼來完成的...
    SeanChense
        13
    SeanChense  
       Aug 29, 2014
    @txx 不好意思 我误会你的意思了
    ld0891
        14
    ld0891  
       Aug 30, 2014
    html代码没有关系,只要你出来的是NSString就行。
    line break mode设置为wordwrap,number of lines设置为0就可以自动适应了。
    oppressed6370
        15
    oppressed6370  
       Aug 30, 2014 via iPhone
    @txx 不过手写autolayout好麻烦啊,xib最方便
    txx
        16
    txx  
       Aug 30, 2014 via iPhone
    @cyokvip 当然不会 谁用原生啊 果断第三方
    oppressed6370
        17
    oppressed6370  
       Aug 31, 2014 via iPhone
    @txx 第三方的一般用什么,求推荐,我才学iOS不久😄
    txx
        18
    txx  
       Aug 31, 2014
    oppressed6370
        19
    oppressed6370  
       Aug 31, 2014 via iPhone
    @txx 额,这个貌似不兼容swift
    ewangke
        20
    ewangke  
       Aug 31, 2014
    @txx 虾神你赶快洗洗睡吧...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3322 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 11:00 · PVG 19:00 · LAX 04:00 · JFK 07:00
    ♥ Do have faith in what you're doing.