UILabel not wrapping text correctly sometimes (auto layout)

前端 未结 3 790
北荒
北荒 2020-12-13 03:56

I have a uilabel setup in a view. It doesn\'t have a width constraint, but its width is instead determined by a leading constraint to the thumbnail image, and a trailing con

3条回答
  •  一整个雨季
    2020-12-13 04:24

    I had the same problem and solved it using a suggestion from this answer. In a subclass of UILabel I placed this code:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        self.preferredMaxLayoutWidth = self.bounds.size.width;
    }
    

    I don't understand why this is not the default behavior of UILabel, or at least why you cannot just enable this behavior via a flag.

    I am a little concerned that preferredMaxLayoutWidth is being set in the middle of the layout process, but I see no easy way around that.

提交回复
热议问题