法海娶了白娘子 發表於 2021-10-14 15:39:00

iOS开发_文字加描边

<h1 id="1自定义uilabel">1、自定义UILabel</h1>
<ul>
<li>
<h2 id="gc_labelh">GC_Label.h</h2>
</li>
</ul>
<pre><code class="language-C">#import &lt;UIKit/UIKit.h&gt;

NS_ASSUME_NONNULL_BEGIN

@interface GC_Label : UILabel

// 设置文字描边:默认不描边,设置了描边颜色才会描边
/** 描边颜色 */
@property(nonatomic, strong) UIColor *stroke_color;
/** 描边宽度,默认为1 */
@property(nonatomic, assign) CGFloat stroke_width;

@end

NS_ASSUME_NONNULL_END
</code></pre>
<ul>
<li>
<h2 id="gc_labelm">GC_Label.m</h2>
</li>
</ul>
<pre><code class="language-C">#import "GC_Label.h"

@implementation GC_Label

- (instancetype)initWithFrame:(CGRect)frame {
    self = ;
    if (self) {
      
      self.backgroundColor = Clear_Color;
      
      self.stroke_width = 1;
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect {
   
    if (self.stroke_color) {
      UIColor *temp_color = self.textColor;
      CGContextRef context = UIGraphicsGetCurrentContext();
      // 设置描边宽度
      CGContextSetLineWidth(context, self.stroke_width);
      CGContextSetLineJoin(context, kCGLineJoinRound);
      CGContextSetTextDrawingMode(context, kCGTextStroke);
      // 描边颜色
      self.textColor = self.stroke_color;
      ;
      // 文本颜色
      self.textColor = temp_color;
      CGContextSetTextDrawingMode(context, kCGTextFill);
      ;
    }
    else {
      ;
    }
}

@end
</code></pre>
<h1 id="2使用与效果">2、使用与效果</h1>
<ul>
<li>
<h2 id="使用">使用</h2>
</li>
</ul>
<pre><code class="language-C">_title_tv = [ init];
_title_tv.textAlignment = NSTextAlignmentCenter;
_title_tv.textColor = ;

_title_tv.stroke_color = ;
_title_tv.stroke_width = 5;

_title_tv.text = @"温度26℃";
</code></pre>
<ul>
<li>
<h2 id="效果">效果</h2>
</li>
</ul>
<p><img src="https://img2020.cnblogs.com/blog/1213778/202110/1213778-20211014153742560-1230541319.png" alt="" loading="lazy"></p>


</div>
<div id="MySignature" role="contentinfo">
    <hr>
<br>
<div>作者: CH520</div>
<div>出处: 博客园资源分享中心</div><br><br>
来源:https://www.cnblogs.com/CH520/p/15406972.html
頁: [1]
查看完整版本: iOS开发_文字加描边