美文网首页iOS常用
iOS 文字横竖排切换

iOS 文字横竖排切换

作者: 达若漠沙 | 来源:发表于2020-06-19 13:04 被阅读0次

    使用YYText 的YYLabel 实现文字横竖排版时发现一个问题,当对verticalForm 设置为YES时,也就是文字竖排时。文字的颜色总会变成默认的黑色。当然有一种情况除外,那就是横排颜色和竖排颜色不一致时,设置竖排颜色才会有效,而不至于是默认的黑色。
    特意看了一下对TextColor赋值的逻辑

    - (void)setTextColor:(UIColor *)textColor {
        if (!textColor) {
            textColor = [UIColor blackColor];
        }
        if (_textColor == textColor || [_textColor isEqual:textColor]) return;
        _textColor = textColor;
        _innerText.yy_color = textColor;
        if (_innerText.length && !_ignoreCommonProperties) {
            if (_displaysAsynchronously && _clearContentsBeforeAsynchronouslyDisplay) {
                [self _clearContents];
            }
            [self _setLayoutNeedUpdate];
        }
    }
    

    看样子,如果不改变颜色,那么正常来讲会通过这里直接return

    if (_textColor == textColor || [_textColor isEqual:textColor]) return;
    

    那么,为了避免横竖排切换时,颜色不统一,在进行横竖排切换时,对触发竖排时,对textcolor多设置一次值,然后再赋值为之前的颜色,那么就能够让这两次设置的颜色都跳过reture,顺利赋值成功

       if (sender.on) {
    //        _preTextLabel.textColor = [UIColor whiteColor];
            [_preTextLabel setTextColor:[UIColor clearColor]];
            [_preTextLabel setTextColor:_currentColor];
        }
    

    横排的时候不需要,至此,在进行横竖排切换时,颜色就保持一致了。

    相关文章

      网友评论

        本文标题:iOS 文字横竖排切换

        本文链接:https://www.haomeiwen.com/subject/ntyvxktx.html