回答
你可以嘗試用圖像設置顏色的標籤,
添加以下方法
func getGradientImage(_ bounds:CGRect) -> UIImage {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [
UIColor(red: 0.596, green: 0.839, blue: 0.929, alpha: 1.00).cgColor,
UIColor(red: 0.169, green: 0.302, blue: 0.408, alpha: 1.00).cgColor
]
gradientLayer.startPoint = CGPoint.zero
gradientLayer.endPoint = CGPoint(x: 1, y: 1) // changing start and end point value you can set vertical or horizontal
gradientLayer.locations = [0.5,1]
gradientLayer.bounds = bounds
UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, true, 0.0)
let context = UIGraphicsGetCurrentContext()
gradientLayer.render(in: context!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
並設置顏色爲您的標籤一樣
yourLabel.textColor = UIColor(patternImage: getGradientImage(yourLabel.bounds))
感謝您的想法 –
我的榮幸。 – dip
是的,你可以跟CoreGarphics的幫助:
步驟一:
下面的功能讓你的文字BezierPath - >
-(UIBezierPath*)getPathOfStr{
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:64];
CGFontRef fontref = CGFontCreateWithFontName((__bridge CFStringRef)font.fontName);
NSString *unichars = @"I";
CFStringRef yourFriendlyCFString = (__bridge CFStringRef)unichars;
CGGlyph glyphs = CGFontGetGlyphWithGlyphName(fontref, yourFriendlyCFString);
CTFontRef fontCT = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
CGPathRef cgpath = CTFontCreatePathForGlyph(fontCT, glyphs, nil);
UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:cgpath];
NSLog(@"Complete path For p is %@", path);
CGPathApply(cgpath, (__bridge void * _Nullable)(bezierPoints), MyCGPathApplierFunc);
NSLog(@"Points on path %@", bezierPoints);
return path;
}
第二步:
現在製作一個漸變圖層,並用您的文本路徑將其遮罩,使用填充顏色=清除顏色,以便漸變圖層將從文本中可見。
這是在目標C或迅速答案? – KKRocks
對不起,犯了一個錯誤。 代碼編輯。 –
- 1. 如何垂直分割2種顏色的文字?
- 2. UILabel TEXT的兩種顏色
- 3. UILabel中的多種顏色
- 4. 的UILabel用兩種不同顏色
- 5. 垂直顏色梯度
- 6. 垂直對齊UILabel
- 7. 繪畫行,只使用2種顏色
- 8. Pyplot axvspan:一個範圍內的多種顏色(垂直)
- 9. 顏色混合超過2種顏色
- 10. 改變顏色,2種顏色
- 11. 顏色1和顏色2的n種顏色的漸變
- 12. Qt垂直滑塊的顏色
- 13. Eclipse:改變垂直條的顏色
- 14. UILabel垂直設置文本
- 15. UILabel TextAlignement垂直頂部
- 16. 多個垂直背景顏色CSS3
- 17. 垂直CSS分隔線顏色
- 18. 垂直滾動條顏色不變
- 19. PHP交替單元格顏色垂直
- 20. 使用Dygraph在圖形中更改垂直條的顏色
- 21. java鞦韆邊框,使用2種或多種顏色
- 22. 使用記事本++如何更改垂直輪廓顏色?
- 23. 使用matplotlib使用多種顏色的顏色軸脊柱
- 24. 爲什麼BFS使用2種顏色標記節點,而使用3種顏色的DFS?
- 25. 如何垂直居中2的UILabel與動態高度
- 26. Android - 帶2種不同顏色的ListView
- 27. 直接從高亮顏色切換顏色到另一種顏色
- 28. 垂直對齊UILabel中的文本
- 29. 垂直對齊UILabel中的文字(xcode)
- 30. 禁用白色垂直線
試試這個HTTPS ://www.cocoacontrols.com/controls/leffectlabel –
使用CAGra在標籤上添加子圖層dientLayer –
試試這個鏈接它工作正常:http://stackoverflow.com/a/41316298/3901620 – KKRocks