text - UIButton টেক্সট আন্ডারলাইন
formatting (12)
যে কেউ একটি UIButton শিরোনাম আন্ডারলাইন কিভাবে সুপারিশ করতে পারেন? আমার নিজস্ব কাস্টম টাইপের UIButton আছে, এবং আমি শিরোনামটিকে আন্ডারলাইন করতে চাই, তবে ইন্টারফেস বিল্ডার এটি করার জন্য কোনও বিকল্প সরবরাহ করে না।
ইন্টারফেস বিল্ডারে যখন আপনি একটি বোতামের জন্য ফন্ট অপশনটি নির্বাচন করেন, তখন এটি কোনও, একক, ডাবল, রঙ নির্বাচন করার বিকল্প সরবরাহ করে না তবে এগুলির মধ্যে কেউ বোতামের শিরোনামে কোনও পরিবর্তন প্রদান করে না।
কোন সাহায্য প্রশংসা।
@ নিক এইচ 247 এর উত্তরটি প্রসারিত করে, আমি এমন একটি সমস্যা অনুভব করি যেখানে বোতামটি পুনরাবৃত্তি করার সময় প্রথমত নিম্নরেখাটি পুনরায় লোড করা হয় নি; এইভাবে আপনার বাটন সেটিকে পুনরায় লোড করার মাধ্যমে সমাধান করা যেতে পারে:
myButton.contentMode = UIViewContentModeRedraw;
সীমানা পরিবর্তন যখন এই বাটন বাড়াতে বাধ্য।
দ্বিতীয়ত, আসল কোডটি অনুমান করে যে আপনি বোতামে শুধুমাত্র 1 টি লাইনের পাঠ্য (আমার বোতামটি ঘূর্ণায়মান 2 লাইনে আবৃত) এবং আন্ডারলাইনে শুধুমাত্র পাঠ্যের শেষ লাইনে প্রদর্শিত হবে। বোতামে লাইনের সংখ্যা গণনা করার জন্য ড্ররেক্ট কোডটি সংশোধন করা যেতে পারে, তারপরে নীচের লাইকের পরিবর্তে প্রতি লাইনে একটি নিম্নরেখা স্থাপন করুন, যেমন:
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);
CGSize labelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
constrainedToSize:self.titleLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGSize labelSizeNoWrap = [self.titleLabel.text sizeWithFont:self.titleLabel.font forWidth:self.titleLabel.frame.size.width lineBreakMode:UILineBreakModeMiddleTruncation ];
int numberOfLines = abs(labelSize.height/labelSizeNoWrap.height);
for(int i = 1; i<=numberOfLines;i++) {
// Original code
// CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender + PADDING);
//
// CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + (labelSizeNoWrap.height*i) + descender + PADDING);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + (labelSizeNoWrap.height*i) + descender);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
}
এই কোড অন্য কেউ সাহায্য আশা করি!
UIUnderlinedButton.h
@interface UIUnderlinedButton : UIButton {
}
+ (UIUnderlinedButton*) underlinedButton;
@end
UIUnderlinedButton.m
@implementation UIUnderlinedButton
+ (UIUnderlinedButton*) underlinedButton {
UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
return [button autorelease];
}
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
@end
আন্ডারলাইনের জন্য ইন্টারফেস বিল্ডার ব্যবহার করার জন্য, একটি আছে:
- এটি দায়ী পরিবর্তন করুন
- গুণাবলী পরিদর্শক মধ্যে টেক্সট হাইলাইট
- ডান ক্লিক করুন, ফন্ট নির্বাচন করুন এবং তারপর আন্ডারলাইন
অন্য কেউ ভিডিও তৈরি করেছেন https://www.youtube.com/watch?v=5-ZnV3jQd9I
এখানে আমার ফাংশন, সুইফ্ট 1.2 কাজ করে।
func underlineButton(button : UIButton, text: String) {
var titleString : NSMutableAttributedString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, count(text.utf8)))
button.setAttributedTitle(titleString, forState: .Normal)
}
আপডেট সুইফট 3.0 এক্সটেনশান:
extension UIButton {
func underlineButton(text: String) {
let titleString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
self.setAttributedTitle(titleString, for: .normal)
}
}
এটি গুণিত স্ট্রিং সঙ্গে খুব সহজ
সেট বৈশিষ্ট্য সহ একটি অভিধান তৈরি করে এবং প্রদত্ত স্ট্রিং প্রয়োগ করুন। তারপরে আপনি ইউটিবেলে ইউটিউব্টন বা এট্রিবিউটেটেটেক্সিতে গুণিত স্ট্রিংটি এটিবিউটেটিশ হিসাবে সেট করতে পারেন।
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont
systemFontOfSize:14.0],NSForegroundColorAttributeName : [UIColor
whiteColor]};
NSMutableAttributedString *title =[[NSMutableAttributedString alloc] initWithString:@"mybutton" attributes: attrDict];
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,[commentString length])]; [btnRegLater setAttributedTitle:title forState:UIControlStateNormal];
দ্রুত
func underlineButton(button : UIButton) {
var titleString : NSMutableAttributedString = NSMutableAttributedString(string: button.titleLabel!.text!)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, button.titleLabel!.text!.utf16Count))
button.setAttributedTitle(titleString, forState: .Normal)}
নিক H247 এর উত্তর কিন্তু সুইফ্ট পদ্ধতির:
import UIKit
class UnderlineUIButton: UIButton {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let textRect = self.titleLabel!.frame
var descender = self.titleLabel?.font.descender
var contextRef: CGContextRef = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel?.textColor.CGColor);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender!);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender!);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
}
যখন আমরা বোতামটি আন্ডারলাইনে চাপা রাখি তখন কীভাবে কেসটি পরিচালনা করব? যে ক্ষেত্রে বোতাম এর টেক্সট রঙ হাইলাইট রঙ অনুযায়ী পরিবর্তন কিন্তু মূল রঙ লাইন অবশেষ। বলুন স্বাভাবিক অবস্থায় বাটন টেক্সট রঙ কালো হলে তার আন্ডারলাইনে কালো রঙ থাকবে। বাটন এর হাইলাইট রঙ সাদা। বোতাম ধরে রাখা পরিবর্তনগুলি কালো থেকে সাদা থেকে বাটন পাঠ্য রঙ পরিবর্তন করে কিন্তু আন্ডারলাইন রঙ কালো থাকে।
সুইফ্ট 3 এর জন্য নিম্নলিখিত এক্সটেনশন ব্যবহার করা যেতে পারে:
extension UIButton {
func underlineButton(text: String) {
let titleString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
self.setAttributedTitle(titleString, for: .normal)
}
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
UIColor *colr;
// set to same colour as text
if (self.isHighlighted || self.isSelected) {
colr=self.titleLabel.highlightedTextColor;
}
else{
colr= self.titleLabel.textColor;
}
CGContextSetStrokeColorWithColor(contextRef, colr.CGColor);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
//Override this to change the underline color to highlighted color
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
// [self setNeedsDisplay];
}
func underline(text: String, state: UIControlState = .normal, color:UIColor? = nil) {
var titleString = NSMutableAttributedString(string: text)
if let color = color {
titleString = NSMutableAttributedString(string: text,
attributes: [NSForegroundColorAttributeName: color])
}
let stringRange = NSMakeRange(0, text.characters.count)
titleString.addAttribute(NSUnderlineStyleAttributeName,
value: NSUnderlineStyle.styleSingle.rawValue,
range: stringRange)
self.setAttributedTitle(titleString, for: state)
}