【问题标题】:Add image to the left of a UILabel将图像添加到 UILabel 的左侧
【发布时间】:2012-12-05 14:23:09
【问题描述】:

我想我有一个相对简单的问题。 想象一下,我有一个UILabel,里面有一些文字。然后,我想在左边,或者右边 文本也是要显示(添加)的图像。

类似这样的:

http://www.zazzle.com/blue_arrow_button_left_business_card_templates-240863912615266256

有没有办法做到这一点,比如使用UILabel 方法?没找到。

【问题讨论】:

  • 它叫uiimageview。
  • 我知道,但是在我的功能中(请同时查看我链接的图像),我基本上希望将 uiimageview(或只是图像)嵌入到标签中。我清楚了吗?谢谢。我希望它们都在一个控件中。例如,我希望 UILabel 有这样的方法: myLabel.addImageToTheLeft()... 等。
  • 我在这里发布了类似问题的答案:stackoverflow.com/a/31112219/553939 它是用 swift 编写的,但代码很容易转换为 Obj-C

标签: ios cocoa-touch


【解决方案1】:

创建一个包含 UIImageView 和 UILabel 子视图的自定义 UIView。您必须在其中执行一些几何逻辑来调整标签的大小以适合左侧或右侧的图像,但不应过多。

【讨论】:

  • 您好,谢谢。所以没有内置的方法来做到这一点?实际上,现在我正在像你说的那样做某事。我有一个视图和两个子视图:一个 UILabel 和一个 UIimageView(都使用 Interface builder 相应地定位)。我认为这应该这样工作?谢谢。
【解决方案2】:

用你的图片创建一个 UIImageView 并在顶部添加 UILabel

[imageview addSubView:label];

根据您需要的位置设置标签的框架。

【讨论】:

    【解决方案3】:

    以防万一其他人将来查看此内容。我会继承 UIlabel 类并添加一个图像属性。

    然后你可以覆盖 text 和 image 属性的设置器。

    - (void)setImage:(UIImage *)image {
        _image = image;
    
        [self repositionTextAndImage];
    }
    
    - (void)setText:(NSString *)text {
        [super setText:text];
    
        [self repositionTextAndImage];
    }
    

    在 repositionTextAndImage 中,您可以进行定位计算。我贴的代码,在左边插入一张图片就行了。

    - (void)repositionTextAndImage {
        if (!self.imageView) {
            self.imageView = [[UIImageView alloc] init];
            [self addSubview:self.imageView];
        }
    
        self.imageView.image = self.image;
        CGFloat y = (self.frame.size.height - self.image.size.height) / 2;
        self.imageView.frame = CGRectMake(0, y, self.image.size.width, self.image.size.height);
    }
    

    最后,覆盖 drawTextInRect: 并确保在标签左侧留出空间,使其不会与图像重叠。

    - (void)drawTextInRect:(CGRect)rect {
        // Leave some space to draw the image.
        UIEdgeInsets insets = {0, self.image.size.width + kImageTextSpacer, 0, 0};
        [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
    }
    

    【讨论】:

    • 我最近刚刚使用了这个方法,想补充一些注意事项:为了让 IB 中的内在内容大小正确地为这个标签子类工作,你还应该重写 intrinsicContentSize 方法来解决额外的问题宽度。此外,使用带有附件的 NSAttributedString 可能是一种更理想的方法。
    【解决方案4】:

    我刚刚在我的 Live Project 中实现了类似的东西,希望它会有所帮助。

    -(void)setImageIcon:(UIImage*)image WithText:(NSString*)strText{
    
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = image;
        float offsetY = -4.5; //This can be dynamic with respect to size of image and UILabel
        attachment.bounds = CGRectIntegral( CGRectMake(0, offsetY, attachment.image.size.width, attachment.image.size.height));
    
        NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
        NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:strText];
    
        [attachmentString appendAttributedString:myString];
    
        _lblMail.attributedText = attachmentString;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2011-08-06
      相关资源
      最近更新 更多