Muhammad Noamany
Muhammad Noamany

Reputation: 52

Capitalize and enlarge the first letter of TextView in android

How to make the first letter of a text in a textView large and capital using android studio like the one in the attached image.

this is the image

Upvotes: 0

Views: 2100

Answers (5)

Sheharyar Ejaz
Sheharyar Ejaz

Reputation: 2858

enter image description here

This 100% works. Here are the steps:

  1. Use this library

    compile 'com.novoda:drop-cap:1.1.0'
    
  2. Define it in your layout

    <com.novoda.dropcap.DropCapView
      android:id="@+id/view_drop_cap"
      style="@style/DropCap"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/button1_detail" />
    
  3. Define it into your styles.xml

    <style name="DropCap">
      <item name="android:paddingLeft">@dimen/drop_cap_padding_left</item>
      <item name="android:paddingTop">@dimen/drop_cap_padding_top</item>
      <item name="android:paddingRight">@dimen/drop_cap_padding_right</item>
      <item name="android:paddingBottom">@dimen/drop_cap_padding_bottom</item>
      <item name="dropCapTextSize">@dimen/drop_cap_text</item>
      <item name="numberOfDropCaps">1</item>
      <item name="dropCapFontPath">fonts/SANS-SERIF_Cabin-Regular.otf</item>
      <item name="copyTextSize">@dimen/copy_text</item>
      <item name="copyFontPath">fonts/neuropolitical_rg.ttf</item>
      <item name="lineSpacingExtra">@dimen/drop_cap_linespacing_extra</item>
    </style>
    
  4. Define it in your dimens.xml

    <dimen name="drop_cap_padding_left">10dp</dimen>
    <dimen name="drop_cap_padding_top">10dp</dimen>
    <dimen name="drop_cap_padding_right">10dp</dimen>
    <dimen name="drop_cap_padding_bottom">10dp</dimen>
    <dimen name="drop_cap_text">64sp</dimen>
    <dimen name="copy_text">21sp</dimen>
    <dimen name="scroll_view_height">200dp</dimen>
    <dimen name="divider_height">1dp</dimen>
    <dimen name="drop_cap_linespacing_extra">0sp</dimen>
    
  5. In your Java code, just findItById() and set Text.

Reference -> https://github.com/novoda/drop-cap

Upvotes: 1

Ramanjeet Singh
Ramanjeet Singh

Reputation: 11

Capitalize and enlarge the first letter of the TextView in Swift iOS UIKit:

// Create a textView First
let textView = UITextView(frame:self.view.bounds)
self.view.addSubView(textView)
textView.attributedText = "Add Attributed Text here"

// Add this dropCap label
let dropCapLabel = UILabel(frame: CGRect(x: 10, y: 5, width: 50, height: 70))
dropCapLabel.text = String("A") // "Add first character here
dropCapLabel.font = UIFont.systemFont(ofSize: 10,weight: .bold)
dropCapLabel.backgroundColor = .clear
textview.addSubview(dropCapLabel)

textview.textContainer.exclusionPaths = [
  UIBezierPath(rect: dropCapLabel.frame)
]

    

Screenshot

Upvotes: 0

Akash Arora
Akash Arora

Reputation: 71

You can use one textView for character A and another textView for rest of text. i don't think is there any code to write like this because of alignment.

Upvotes: 0

EKN
EKN

Reputation: 1906

Please try this code snippet, this will help you.

String str = "sample text";

//Change first character to capital letter
String tempStr = str.substring(0, 1).toUpperCase() + str.substring(1);

//Change font size of the first character. You can change 2f as you want
SpannableString spannableString = new SpannableString(tempStr);
spannableString.setSpan(new RelativeSizeSpan(2f), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//Set the formatted text to text view
tvSample.setText(spannableString);

Upvotes: 2

user8232051
user8232051

Reputation:

You can use this :

String upperString = myString.substring(0,1).toUpperCase() + myString.substring(1);

Upvotes: 1

Related Questions