How to display dynamic multiple label in tableview cell swift? - ios

I'm trying with NSMutableAttributedString but not getting perfect resolution and also underline for each string.
Now It's display like this:
But I want like this:
I m trying with this code in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) for displaying the cell:
let arrSearchTag = search_detail?.value(forKey: "search_tags") as! NSArray
if arrSearchTag.count > 0
{
let str : NSMutableAttributedString = NSMutableAttributedString(string: "")
for string in arrSearchTag
{
let myString = string
let myString1 = " "
let myAttribute = [NSBackgroundColorAttributeName : UIColor.init(red: 94.0/255.0, green: 94.0/255.0, blue: 94.0/255.0, alpha: 1.0)]
let myAttribute1 = [NSBackgroundColorAttributeName : UIColor.clear]
let range = (myString as! NSString).range(of: myString as! String)
let myAttrString = NSMutableAttributedString(string: myString as! String, attributes: myAttribute)
let myAttrString1 = NSMutableAttributedString(string: myString1 , attributes: myAttribute1)
myAttrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: range)
str.append(myAttrString)
str.append(myAttrString1)
}
cell.lblSearchTags.attributedText = str
}

Related

Add text with different style to a existing label

I have a label with text and want to add more test in running time, but I want to add different style to this text that was add. Is there a way to do this?
This is the code
label.text = (label.text ?? "") + " \n \(userName)"
How do I add style to userName without changing the style of the label?
use attributed text in UILabel:
here some code.
a) some useful typedefs:
typealias AttrDict = [NSAttributedString.Key : Any]
a) create some styles:
func textAttribute() -> AttrDict{
let textFont = UIFont.systemFont(ofSize: 32)
let stdAttrib = [NSAttributedString.Key.font: textFont,
//NSParagraphStyleAttributeName: paragraphStyle2,
NSAttributedString.Key.foregroundColor: UIColor.red] as AttrDict
return stdAttrib
}
func smallTextAttribute() -> AttrDict{
let textFont = UIFont.systemFont(ofSize: 10)
let stdAttrib = [NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: UIColor.green] as AttrDict
return stdAttrib
}
c) build Your attributed String:
func myAttributedString() -> NSAttributedString {
let pieces = ["hello", "word"]
let resultAttributed = NSMutableAttributedString()
var s = ""
s = pieces[0] + "\n"
resultAttributed.append(NSAttributedString(string: s,
attributes: stdTextAttribute() ))
s = pieces[1] + "\n"
resultAttributed.append(NSAttributedString(string: s,
attributes: smallTextAttribute() ))
return resultAttributed
}
d) put in Your label/textView:
....
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myLabel.numberOfLines = 0
myLabel.attributedText = myAttributedString()
}
}
I made a GIST:
https://gist.github.com/ingconti/aefc78c6d0b22f5329f906094c312a21
PLS connect UIlabel..
:)
To do this, you need to use NSMutableAttributedString, NSAttributedString and use label.attributedText, How?
Doing this:
let userName = "StackOverflow"
let prefixText = NSAttributedString(string: "this is normal ")
let font = UIFont.systemFont(ofSize: 40)
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: UIColor.blue
]
let userNameWithStyle = NSAttributedString(string: userName, attributes: attributes)
let finalString = NSMutableAttributedString(attributedString: prefixText)
finalString.append(userNameWithStyle)
self.label.attributedText = finalString
Image result

Format data content in Tableviewcell in Swift

How it should look like.
How it currently look like.
I'm trying to display information on my tableview cell.
The first cell information on the image above looks good, but only when the table view is on edit mode - and I only want the same display in normal mode for all my cells.
Here is my code below
//cellForRow method
var cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: celliD)
cell?.textLabel?.numberOfLines = 0
cell?.detailTextLabel?.numberOfLines = 0
cell?.accessoryView = nil
cell?.tintColor = UIColor.cmoOrangeColor
cell?.textLabel?.textColor = UIColor.darkGray
cell?.detailTextLabel?.textColor = UIColor.lightGray
let animalEntity = self.newSaleSelectedAnimalsManagedObject[indexPath.row] as? AnimalEntity
cell?.textLabel?.attributedText = AnimalFacade.animalTitleAttributedText(animalEntity: animalEntity!, withHolding: false)
cell?.detailTextLabel?.attributedText = AnimalFacade.animalDetails()
return cell!
//textLabel formating data
class func animalTitleAttributedText(animalEntity: AnimalEntity, withHolding: Bool) -> NSMutableAttributedString {
//AttributedText
let attributedTitle = NSDictionary(object: UIFont.tableViewCellTitle as Any, forKey: NSFontAttributeName as NSCopying) as? [String : Any]
let attributedSubtitle = NSDictionary(object: UIFont.tableViewCellSubtitle as Any, forKey: NSFontAttributeName as NSCopying) as? [String : Any]
//TextLabel
let titleAttributedText = NSMutableAttributedString()
//officialTag
let officialTag = animalEntity.officialTag //UK 123456 123456
let mgtTag = animalEntity.managementTag //WER3456
let age = DateUtil.calculateAge(dateTime: animalEntity.dob! as Date) //13 y
let animalCategory = animalEntity.animalTag //Heifer Calf
let breed = animalEntity.breed //AB
titleAttributedText.append(NSAttributedString(string: officialTag! + "\n", attributes: attributedTitle))
titleAttributedText.append(NSAttributedString(string: "Mgmt Tag: \(mgtTag!), \(age), \(animalCategory!), \(breed!) \n", attributes: attributedSubtitle))
return titleAttributedText
}
//detailLabel formatting data
class func animalDetails() -> NSMutableAttributedString {
let attributedSubtitle = NSDictionary(object: UIFont.tableViewCellSubtitle as Any, forKey: NSFontAttributeName as NSCopying) as? [String : Any]
//detailsLabel
let detailsAttributedText = NSMutableAttributedString()
let price = "1234.85"
let lotNumber = "TREPOU123"
detailsAttributedText.append(NSAttributedString(string: price + "\n\n", attributes: attributedSubtitle))
detailsAttributedText.append(NSAttributedString(string: lotNumber, attributes: attributedSubtitle))
return detailsAttributedText
}

didSelectRowAt indexPath method called after long pressing the UITableViewCell

I'm developing a simple shopping app. There I used UITableView for listing the product but when I select the listed product in the particular cell, "didSelectRowAt indexPath" method has not been called/triggered, so I tried with long pressing UITableViewCell the method is working good and also triggered properly but I don't need this solution.
In the product listing page, I just used UISegmentedControl in the navigation bar and dynamic prototyped UITableView in the UIView. Even I didn't use any kind of UIGestures in this page.
This question may be duplicated and already asked in Stack Overflow, but there were none of the answers solved my problem. I need to continue working the UITableViewCell without long pressing.
Note: I tried switching off the "Delays Content Touches" in TableView, and I surf through Stack Overflow and web but I couldn't find any solutions.
Update
"cellForRowAt indexPath" code are as follows:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ProductCell
let vendor = array_list_vendor[indexPath.row] as? String
let orgPrice = array_list_price[indexPath.row] as? Int
let starDealPrice = array_list_stardealPrice[indexPath.row] as? Int
let url = array_list_image[indexPath.row] as! String
cell.productTitle.text = array_list_title[indexPath.row] as? String
if url == "" {
cell.productImg.image = UIImage(named: "no-img")
}
else {
cell.productImg.image = nil
ImageLoader.sharedLoader.imageForUrl(urlString: (url), completionHandler:{(image: UIImage?, url: String) in
//cell.productImg.frame.width =
cell.productImg.image = image!
})
}
if vendor == "amazon" {
cell.sellerImg.image = UIImage(named: "prodicon-amazon")
}
else if vendor == "flipkart" {
cell.sellerImg.image = UIImage(named: "prodicon-flipkart")
}
else {
cell.sellerImg.image = UIImage(named: "prodicon-amazon")
}
cell.starDealImg.image = UIImage(named: "prodicon-stardeal")
let rupee = "\u{20B9}"
let text = String(format: "%# %d", rupee, orgPrice!)
let linkTextWithColor = String(rupee)
let range = (text as NSString).range(of: linkTextWithColor!)
let attributedString = NSMutableAttributedString(string:text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.black , range: range)
//Seller price
cell.sellerPrice.attributedText = attributedString
let text1 = String(format: "%# %d", rupee, starDealPrice!)
let linkTextWithColor1 = String(rupee)
let range1 = (text1 as NSString).range(of: linkTextWithColor1!)
let attributedString1 = NSMutableAttributedString(string:text1)
attributedString1.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 127.0/255.0, green: 63.0/255.0, blue: 152.0/255.0, alpha: 1.0) , range: range1) //Purple color
//Stardeal price
cell.starDealPrice.attributedText = attributedString1
let cashBack = orgPrice! - starDealPrice!
let text2 = String(format: "Save %#", rupee)
let linkTextWithColor2 = String(rupee)
let range2 = (text2 as NSString).range(of: linkTextWithColor2!)
let attributedString2 = NSMutableAttributedString(string:text2)
attributedString2.addAttribute(NSForegroundColorAttributeName, value: UIColor.white , range: range2)
let text3 = String(format: "%d", cashBack)
let linkTextWithColor3 = String(cashBack)
let range3 = (text3 as NSString).range(of: linkTextWithColor3)
let attributedString3 = NSMutableAttributedString(string:text3)
attributedString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.white , range: range3)
//Attributed string with multiple words
attributedString2.append(attributedString3)
cell.productCashBack.attributedText = attributedString2
return cell
}

Make part of a string bold that matches a search string

I have a table of items and each item has a label. I also have a search bar that is used to filter the items in the table based on whether mySearchBar.text is a substring of myLabel.text.
That is all working fine, but I'd like to bold the portions of label text that match the search string.
The end product would be something similar to Google Maps search.
Swift 4 : XCode 9.x
private func filterAndModifyTextAttributes(searchStringCharacters: String, completeStringWithAttributedText: String) -> NSMutableAttributedString {
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: completeStringWithAttributedText)
let pattern = searchStringCharacters.lowercased()
let range: NSRange = NSMakeRange(0, completeStringWithAttributedText.characters.count)
var regex = NSRegularExpression()
do {
regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options())
regex.enumerateMatches(in: completeStringWithAttributedText.lowercased(), options: NSRegularExpression.MatchingOptions(), range: range) {
(textCheckingResult, matchingFlags, stop) in
let subRange = textCheckingResult?.range
let attributes : [NSAttributedStringKey : Any] = [.font : UIFont.boldSystemFont(ofSize: 17),.foregroundColor: UIColor.red ]
attributedString.addAttributes(attributes, range: subRange!)
}
}catch{
print(error.localizedDescription)
}
return attributedString
}
How to use :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle , reuseIdentifier: "Cell")
cell.textLabel?.attributedText = self.filterAndModifyTextAttributes(searchStringCharacters: self.textFromSearchBar, completeStringWithAttributedText: searchResultString)
return cell
}
Here's a sample of what I ended up implementing:
#IBOutlet weak var mySearchBar: UISearchBar!
#IBOutlet weak var myLabel: UILabel!
...
func makeMatchingPartBold(searchText: String) {
// check label text & search text
guard
let labelText = myLabel.text,
let searchText = mySearchBar.text
else {
return
}
// bold attribute
let boldAttr = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: myLabel.font.pointSize)]
// check if label text contains search text
if let matchRange: Range = labelText.lowercased().range(of: searchText.lowercased()) {
// get range start/length because NSMutableAttributedString.setAttributes() needs NSRange not Range<String.Index>
let matchRangeStart: Int = labelText.distance(from: labelText.startIndex, to: matchRange.lowerBound)
let matchRangeEnd: Int = labelText.distance(from: labelText.startIndex, to: matchRange.upperBound)
let matchRangeLength: Int = matchRangeEnd - matchRangeStart
// create mutable attributed string & bold matching part
let newLabelText = NSMutableAttributedString(string: labelText)
newLabelText.setAttributes(boldAttr, range: NSMakeRange(matchRangeStart, matchRangeLength))
// set label attributed text
myLabel.attributedText = newNameText
}
}

Bold part of a UIButton title label

I've tried a couple of different methods and extensions after coming across them on S.O. to no avail. Is there a definitive way to bold only part of a UIButton.titleLabel?
These are some of the extensions I've tried:
func attributedText(fullStr: String, boldStr: String) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: fullStr as String, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(12.0)]
// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: NSMakeRange(0, boldStr.characters.count))
return attributedString
}
func boldRange(range: Range<String.Index>) {
if let text = self.attributedTitleForState(UIControlState.Normal) {
let attr = NSMutableAttributedString(attributedString: text)
let start = text.string.startIndex.distanceTo(range.startIndex)
let length = range.startIndex.distanceTo(range.endIndex)
attr.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(16)], range: NSMakeRange(start, length))
self.setAttributedTitle(attr, forState: UIControlState.Normal)
}
}
func boldSubstring(substr: String) {
let range = substr.rangeOfString(substr)
if let r = range {
boldRange(r)
}
}
Anyone have anything?
Swift 4 version of chicobermuda's answer:
let text = "This is the"
let attr = NSMutableAttributedString(string: "\(text) button's text!")
attr.addAttribute(NSAttributedStringKey.font, value: UIFont.boldSystemFont(ofSize: 14), range: NSMakeRange(0, text.count))
cell.nameLabel.setAttributedTitle(attr, forState: .normal)
// "**This is the** button's text!"
it works fine

Resources